﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using UnityEngine;
using Cemit.PolyProto;

namespace Cemit.NetFramework
{
    [System.Obsolete("已过时，使用NetInput获取网络按键输入")]
    public class PolyInput : INetInput
    {
        InputSet<KeyCode> m_KeySets = new InputSet<KeyCode>();

        [Obsolete]
        InputSet<string> m_BottonSets = new InputSet<string>();

        class InputSet<T>
        {
            public HashSet<T> enableSet = new HashSet<T>();
            public HashSet<T> downSet = new HashSet<T>();
            public HashSet<T> upSet = new HashSet<T>();
        }

        public PolyInput(string playerId)
        {
            //设置按键状态变化时的监听
            PolyInputManager.Instances.SetKeyInputListener(playerId, OnKeyInput);
            //PolyInputManager.Instances.SetBottonInputListener(playerId, OnBottonInput);
        }

        [Obsolete]
        private void OnBottonInput(string botton, InputState state)
        {
            SetInput(m_BottonSets, botton, state);
        }

        private void OnKeyInput(KeyCode key, InputState state)
        {
            SetInput(m_KeySets, key, state);
        }

        /// <summary>
        /// 设置按键状态
        /// </summary>
        private void SetInput<T>(InputSet<T> set, T key, InputState state)
        {
            switch (state)
            {
                //按键抬起
                case InputState.up:
                    if (!set.enableSet.Contains(key))
                    {
                        Debug.LogError(
                            $"在没有收到{key}键按下的消息情况下" +
                            $"收到了{key}键抬起的请求。");
                        return;
                    }

                    set.enableSet.Remove(key);
                    ContainsOneFrame(set.upSet);
                    break;

                //按键按下
                case InputState.down:
                    set.enableSet.Add(key);
                    ContainsOneFrame(set.downSet);
                    break;
            }

            void ContainsOneFrame(HashSet<T> hashSet)
            {
                PolyNetwork.MonoHandle.Invoke(() => hashSet.Add(key));
                PolyNetwork.MonoHandle.InvokeAtNextFrame(() => hashSet.Remove(key));
            }
        }

        [Obsolete]
        public float GetAxis()
        {
            return 0;
        }

        public bool GetKey(KeyCode keyCode)
        {
            return m_KeySets.enableSet.Contains(keyCode);
        }

        public bool GetKeyDown(KeyCode keyCode)
        {
            return m_KeySets.downSet.Contains(keyCode);
        }

        public bool GetKeyUp(KeyCode keyCode)
        {
            return m_KeySets.upSet.Contains(keyCode);
        }

        [Obsolete]
        public bool GetBotton()
        {
            return false;
        }

        [Obsolete]
        public bool GetButtonDown()
        {
            return false;
        }

        [Obsolete]
        public bool GetButtonUp()
        {
            return false;
        }
    }
}
